package com.umbrella.worker.web.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.umbrella.worker.dto.ContactDO;
import com.umbrella.worker.dto.TaskDO;
import com.umbrella.worker.query.ContactQuery;
import com.umbrella.worker.result.DateValueDO;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.IContactService;


@Controller
@RequestMapping(value = "/goods")
public class GoodsController extends BaseController {
	
	@Autowired
	private IContactService contactService;

	
	@RequestMapping(value = "/clean.html", method = RequestMethod.GET)
	public ModelAndView clean(ModelAndView mav, 
			HttpServletRequest request) {
		String[] weekDays = {"(星期天)", "(星期一)", "(星期二)", "(星期三)", "(星期四)", "(星期五)", "(星期六)"};
		long timestamp = Calendar.getInstance().getTimeInMillis();
		
		
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY); 
		Date date = new Date(timestamp);
		
		DateFormat dateFormat = new SimpleDateFormat("MM月dd日"); 
		DateFormat dateValueFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		
		int w = c.get(Calendar.DAY_OF_WEEK) - 1;
		String currentDay = dateFormat.format(date) + weekDays[w];
		String currentValue = dateValueFormat.format(date);

		mav.addObject("CURRENT_DAY", currentDay);
		String hourValue = "";
		if(hour > 9 && hour < 18) {
			hourValue = "" + (hour += 3) + ":00:00";
		} else if (hour < 9){
			hour = 9;
			hourValue = "" + hour + ":00:00";
		} else {
			date = new Date(timestamp + (10 * 60 * 60 * 1000));
			hour = 9;
			hourValue = "" + hour + ":00:00";
			currentDay = dateFormat.format(date) + weekDays[w];
		}
		mav.addObject("CURRENT_TIME", hourValue);
		mav.addObject("CURRENT_VALUE", currentValue);
		mav.setViewName("goods/clean");
		return mav;
	}
	
	@RequestMapping(value = "/getTime/{date}.json", method = RequestMethod.GET)
	public ModelAndView getTimeAjax(ModelAndView mav, 
			@PathVariable(value="date") String datestr,
			HttpServletRequest request) {
		String[] weekDays = {"(星期天)", "(星期一)", "(星期二)", "(星期三)", "(星期四)", "(星期五)", "(星期六)"};
		long timestamp = Calendar.getInstance().getTimeInMillis();
		
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY); 
		Date date = new Date(timestamp);
		
		DateFormat dateFormat = new SimpleDateFormat("MM月dd日"); 
		DateFormat dateValueFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		int w = c.get(Calendar.DAY_OF_WEEK) - 1;
		String currentDay = dateFormat.format(date);
		String currentValue = dateValueFormat.format(date);
		String dateHtml = "<ul>";
		for(int day = 1; day < 8; day++) {
			date = new Date(timestamp);
			c.setTime(date);
			w = c.get(Calendar.DAY_OF_WEEK) - 1;
			if(hour >= 18) {
				date = new Date(timestamp + (10 * 60 * 60 * 1000));
			}
			dateHtml += " <li onclick=\"setDate(\'" + dateValueFormat.format(date) + "\', \'" + dateFormat.format(date) + weekDays[w] + "\')\">"+ dateFormat.format(date) + weekDays[w] + "</li>";
			
			timestamp = timestamp + 86400000;
		}
		dateHtml += "</ul>";
		
		Map<String, String> timeValue = new HashMap<String, String>();
		
		String html = "<ul>";
		timeValue.put("CURRENT_VALUE", hour + ":00:00");
		if(currentValue.equals(datestr)) {
			hour += 3;
			if(hour >= 9 && hour <= 18) {
				
				while(hour <= 18) {
					html += "<li onclick=\"setTime(\'" + hour  + ":00:00\')\">" + hour  + ":00:00</li>";
					hour++;
				}
				timeValue.put("CURRENT_DATE", currentDay);
				timeValue.put("CURRENT_TIME", hour  + ":00:00");
				timeValue.put("DATE_LIST", dateHtml);
				timeValue.put("TIME_LIST", html + "</ul>");
				mav.addObject("JSON_DATA", timeValue);
			} else if(hour < 9){
				hour = 9;
				while(hour <= 18) {
					
					html += "<li onclick=\"setTime(\'" + hour  + ":00:00\')\">" + hour  + ":00:00</li>";
					hour++;
				}
				timeValue.put("CURRENT_DATE", currentDay);
				timeValue.put("CURRENT_TIME", "11:00:00");
				timeValue.put("DATE_LIST", dateHtml);
				timeValue.put("TIME_LIST", html + "</ul>");
				mav.addObject("JSON_DATA", timeValue);
			} else {
				while(hour <= 18) {
					html += "<li onclick=\"setTime(\'" + hour  + ":00:00\')\">" + hour  + ":00:00</li>";
					hour++;
				}
				timeValue.put("CURRENT_DATE", currentDay);
				timeValue.put("CURRENT_TIME", "9:00:00");
				timeValue.put("DATE_LIST", dateHtml);
				timeValue.put("TIME_LIST", html + "</ul>");
				mav.addObject("JSON_DATA", timeValue);
			}
		} else {
			hour = 9;
			while(hour <= 18) {
				
				html += "<li onclick=\"setTime(\'" + hour  + ":00:00\')\">" + hour  + ":00:00</li>";
				hour++;
			}
			html += "</ul>";
			timeValue.put("CURRENT_DATE", currentDay);
			timeValue.put("CURRENT_TIME", "9:00:00");
			timeValue.put("DATE_LIST", dateHtml);
			timeValue.put("TIME_LIST", html);
			mav.addObject("JSON_DATA", timeValue);
		}
		System.out.println(html);
		
		return mav;
	}
	
	
	@RequestMapping(value = "/cleanInfo.html", method = RequestMethod.GET)
	public ModelAndView cleanInfo(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("goods/clean_info");
		return mav;
	}
	
	@RequestMapping(value = "/lockInfo.html", method = RequestMethod.GET)
	public ModelAndView lockInfo(ModelAndView mav, 
			HttpServletRequest request) {
		System.out.println("lock_info");
		mav.setViewName("goods/lock_info");
		return mav;
	}
	
	@RequestMapping(value = "/lock.html", method = RequestMethod.GET)
	public ModelAndView lock(ModelAndView mav, 
			HttpServletRequest request) {
		
		mav.setViewName("goods/lock");
		return mav;
	}
	
	@RequestMapping(value = "/pipInfo.html", method = RequestMethod.GET)
	public ModelAndView pipInfo(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("goods/pip_info");
		return mav;
	}
	
	@RequestMapping(value = "/pip.html", method = RequestMethod.GET)
	public ModelAndView pip(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("goods/pip");
		return mav;
	}
	
	@RequestMapping(value = "/toiletInfo.html", method = RequestMethod.GET)
	public ModelAndView toiletInfo(ModelAndView mav, 
			HttpServletRequest request) {
		mav.setViewName("goods/toilet_info");
		return mav;
	}
	
	@RequestMapping(value = "/toilet.html", method = RequestMethod.GET)
	public ModelAndView toilet(ModelAndView mav, 
			HttpServletRequest request) {
		
		mav.setViewName("goods/toilet");
		return mav;
	}
	
	@RequestMapping(value = "/needInfo/{type}.html", method = RequestMethod.GET)
    public ModelAndView needInfo(ModelAndView mav, 
    		@PathVariable(value="type") Integer type,
    		HttpServletRequest request
    		) {
		switch(type) {
		case 1: {
			mav.setViewName("goods/old_info");
			break;
		}
		case 2: {
			mav.setViewName("goods/child_info");
			break;
		}
		case 3: {
			mav.setViewName("goods/nurse_info");
			break;
		}
		case 4: {
			mav.setViewName("goods/mchild_info");
			break;
		}
		}
		
		return mav;
    }
	
	@RequestMapping(value = "/need/{type}.html", method = RequestMethod.GET)
    public ModelAndView need(ModelAndView mav, 
    		@PathVariable(value="type") Integer type,
    		HttpServletRequest request
    		) {
		switch(type) {
		case 1: {
			mav.setViewName("goods/old");
			break;
		}
		case 2: {
			mav.setViewName("goods/child");
			break;
		}
		case 3: {
			mav.setViewName("goods/nurse");
			break;
		}
		case 4: {
			mav.setViewName("goods/mchild");
			break;
		}
		
		}
		return mav;
    }
	
	
	
	@RequestMapping(value = "/buy.html")
	public ModelAndView buy(ModelAndView mav,
			TaskDO taskDO,
			HttpServletRequest request) {

		switch(taskDO.getServiceType()) {
		case 0: {
			taskDO.setServiceName("保洁服务");
			String subTime = taskDO.getSubTime();
			if(subTime.indexOf("18") != -1) {
				taskDO.setPrice(new BigDecimal(40.00));
			}
			//taskDO.setPrice(new BigDecimal(30.00));
			break;
		}
		case 1: {
			taskDO.setServiceName("开锁服务");
			taskDO.setPrice(taskDO.getFee());
			break;
		}
		case 2:{
			taskDO.setServiceName("管道疏通");
			taskDO.setFee(new BigDecimal(100.00));
			taskDO.setPrice(new BigDecimal(100.00));
			break;
		}
		case 3:{
			taskDO.setServiceName("马桶维修");
			taskDO.setFee(new BigDecimal(150.00));
			taskDO.setPrice(new BigDecimal(150.00));
			break;
		}
		case 13: {
			break;
		}
		case 11:{
			taskDO.setServiceName("衣橱整理");
			//这里设置单价

			taskDO.setPrice(new BigDecimal(120.00));
			taskDO.setFee(new BigDecimal(0));

			//衣橱整理预定日期
			String appDate=request.getParameter("appDate");
			String appTime=request.getParameter("appTime");
			
			String wdate="";
			if(appTime!=null && appDate!=""){
				wdate=appDate+" "+appTime;
			}
			mav.addObject("wdate",wdate);
			
			break;
		}
		case 12:{
			taskDO.setServiceName("全屋整理");
			//这里设置单价

			taskDO.setPrice(new BigDecimal(120.00));
			taskDO.setFee(new BigDecimal(0));

			//衣橱整理预定日期
			String appDate=request.getParameter("appDate");
			String appTime=request.getParameter("appTime");
			
			String wdate="";
			if(appTime!=null && appDate!=""){
				wdate=appDate+" "+appTime;
			}
			mav.addObject("wdate",wdate);
			
			break;
		}
		};
		
		Cookie cookie = getCookieByName(request, "MEMBER_ID");
		
		if(cookie == null) {
			return new ModelAndView("redirect:/members/login.html");
		} 
		
		Integer memberId = Integer.parseInt(cookie.getValue());
		request.getSession().setAttribute("TASK_INFO", taskDO);
		
		ContactQuery contactQuery = new ContactQuery();
		contactQuery.setIsDefault(1);
		contactQuery.setMemberId(memberId);
		ResultDO resultDO = contactService.list(contactQuery);
		if(resultDO.isSuccess()) {
			List<ContactDO> list = (List<ContactDO>) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
			if(list.size() != 0) {
				request.getSession().setAttribute("CONTACT", list.get(0));
			} else {
				request.getSession().setAttribute("CONTACT", null);
			}
		}
		mav.setViewName("order/orderInfo");
		
		return mav;
	}

	
	/**
	 * 衣橱整理
	 * @param mav
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/wardrobe.html", method = RequestMethod.GET)
	public ModelAndView wardrobe(ModelAndView mav, 
			HttpServletRequest request) {		

		mav.setViewName("goods/wardrobe");
		return mav;
	}
	
	/**
	 * 衣橱整理
	 * @param mav
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/houseobe.html", method = RequestMethod.GET)
	public ModelAndView houseobe(ModelAndView mav, 
			HttpServletRequest request) {		

		mav.setViewName("goods/houseobe");
		return mav;
	}
	
	//衣橱信息选择
	@RequestMapping(value = "/wardrobeNeed.html", method = RequestMethod.GET)
	public ModelAndView wardrobeNeed(ModelAndView mav, 
			HttpServletRequest request) throws ParseException {
		//衣橱整理预定日期
		String appDate=request.getParameter("appDate");
		String appTime=request.getParameter("appTime");
		
		String wdate="";
		if(appTime!=null && appDate!=""){
			wdate=appDate+" "+appTime;
		}
		mav.addObject("wdate",wdate);
		mav.setViewName("goods/wardrobeNeed");
		return mav;
	}
	
	//衣橱选择时间
	@RequestMapping(value = "/wardrobeTime.html", method = RequestMethod.GET)
	public ModelAndView wardrobeTime(ModelAndView mav, 
			HttpServletRequest request) {
		Calendar c = Calendar.getInstance();
		DateFormat dateValueFormat = new SimpleDateFormat("yyyy-MM-dd"); 
		c.add(Calendar.DAY_OF_MONTH, 1);//当前时间加一天
		String currentValue = dateValueFormat.format(c.getTime());
		
		mav.addObject("nowtime", currentValue);
		DateFormat dateFormat = new SimpleDateFormat("HH:ss"); 
		String hours=dateFormat.format(c.getTime());
		mav.addObject("hourtime", hours);
	
		mav.setViewName("goods/wardrobeTime");
		return mav;
	}
	
	//医院护工
	@RequestMapping(value = "/hospital.html", method = RequestMethod.GET)
	public ModelAndView hospital(ModelAndView mav, 
			HttpServletRequest request) {
		
		mav.setViewName("goods/hospital");
		return mav;
	}
	
	/**
	 * 医院护工预约
	 * @param mav
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/hospitalWorker.html", method = RequestMethod.GET)
	public ModelAndView hospitalWorker(ModelAndView mav, 
			HttpServletRequest request) {
		Date date=new Date();
		DateFormat dateValueFormat = new SimpleDateFormat("yyyy/MM/dd"); 
		String time=dateValueFormat.format(date);
		
		mav.addObject("time", time.trim());
		mav.setViewName("goods/hospitalWorker");
		return mav;
	}
	
	
}
